import { areaList } from '@vant/area-data'
import { ref, type Ref, onMounted } from 'vue'
import { Toast } from 'vant'
import type { IAddressemit } from '../types/type'
import { useAddressList } from '../stores/addressList'
import { useRouter } from 'vue-router'
export const useAddressEmit = () => {
    //返回上一级
    const onClickLeft = () => history.back()
    //保存
    const addressList = useAddressList()
    const router = useRouter()

    //获取数据初始化编辑订单  ---判断
    let form = ref({})
    let areaPlaceholder: any = ref('地区')
     //判断是否有caAddressListrt
    if (localStorage.getItem('caAddressListrt')) {
        const listemit: any = localStorage.getItem('caAddressListrt')
        const res = JSON.parse(listemit).addEmit
        // console.log(res)
        const data = res.address.split('/')
        // console.log(areaPlaceholder)
        const list = {
            name: res.name,
            isDefault: res.isDefault,
            postalCode: res.postalCode,
            tel: res.tel,
            areaCode:res.areaCode,
            addressDetail:res.addressDetail,
        }
        if (!(res.name === '')) {
            form.value = list
        }
    }

    //保存地址
    const onSave = (content: IAddressemit,) => {
        // console.log('///',content);
         const num=addressList.numAdd()
         console.log('content',content)
         
        //  console.log(num)
        let {
            addressDetail,
            city,
            county,
            isDefault,
            name,
            postalCode,
            tel,
            province,
            areaCode

        } = content
        const list: any = {
            name,
            id: num,
            tel,
            addressDetail,
            address: province  + city  + county  + addressDetail,
            isDefault,
            postalCode,
            areaCode,

        }
        addressList.add(list)
        history.back()
        
    }
    
    onMounted(() => {})
    const onDelete = () => Toast('delete')
    return { onClickLeft, onSave, onDelete, areaList, areaPlaceholder, form }
}
